﻿Imports System.Web
Imports System.Text

Namespace DotNetNuke.Modules.ActiveForums
    Public Class PropertiesController
        Friend Function SaveProperty(ByVal pi As PropertiesInfo) As PropertiesInfo
            Dim db As New Data.PropertiesDB
            pi.PropertyId = db.SaveProperty(pi.PropertyId, pi.PortalId, pi.ObjectType, pi.ObjectOwnerId, pi.Name, pi.DataType, pi.DefaultAccessControl, pi.IsHidden, pi.IsRequired, pi.IsReadOnly, pi.ValidationExpression, pi.EditTemplate, pi.ViewTemplate, pi.SortOrder, pi.DefaultValue)
            Return GetProperty(pi.PropertyId, pi.PortalId)
        End Function
        Friend Function ListProperties(ByVal PortalId As Integer, ByVal ObjectType As Integer, ByVal ObjectOwnerId As Integer) As List(Of PropertiesInfo)
            Dim list As New List(Of PropertiesInfo)
            Dim db As New Data.PropertiesDB
            Using dr As IDataReader = db.ListProperties(PortalId, ObjectType, ObjectOwnerId)
                While dr.Read
                    Dim p As PropertiesInfo = FillObject(dr)
                    If Not p Is Nothing Then
                        list.Add(p)
                    End If
                End While
                dr.Close()
            End Using
            Return list
        End Function
        Friend Sub DeleteProperty(ByVal PortalId As Integer, ByVal PropertyId As Integer)
            Dim db As New Data.PropertiesDB
            db.DeleteProperty(PortalId, PropertyId)
        End Sub
        Friend Function ListPropertiesJSON(ByVal PortalId As Integer, ByVal ObjectType As Integer, ByVal ObjectOwnerId As Integer) As String
            Dim list As New List(Of PropertiesInfo)
            list = Me.ListProperties(PortalId, ObjectType, ObjectOwnerId)
            Dim sb As New StringBuilder
            For Each p As PropertiesInfo In list
                sb.Append("{")
                sb.Append(Utilities.JSON.Pair("PropertyId", p.PropertyId.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("PortalId", p.PortalId.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("ObjectType", p.ObjectType.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("ObjectOwnerId", p.ObjectOwnerId.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("Name", p.Name.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("DataType", p.DataType.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("DefaultAccessControl", CInt(p.DefaultAccessControl).ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("IsHidden", p.IsHidden.ToString.ToLowerInvariant))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("IsReadOnly", p.IsReadOnly.ToString.ToLowerInvariant))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("IsRequired", p.IsRequired.ToString.ToLowerInvariant))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("ValidationExpression", HttpUtility.UrlEncode(HttpUtility.HtmlEncode(p.ValidationExpression.ToString))))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("ViewTemplate", p.ViewTemplate.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("EditTemplate", p.EditTemplate.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("SortOrder", p.SortOrder.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("DefaultValue", p.DefaultValue.ToString))
                sb.Append(",")
                sb.Append(Utilities.JSON.Pair("Label", HttpUtility.HtmlEncode("[RESX:" & p.Name & "]")))
                sb.Append("},")

            Next
            If sb.Length > 2 Then
                sb.Remove(sb.Length - 1, 1)
            End If
            Return sb.ToString
        End Function
        Friend Function GetProperty(ByVal PropertyId As Integer, ByVal PortalId As Integer) As PropertiesInfo
            Dim db As New Data.PropertiesDB
            Dim pi As PropertiesInfo = Nothing
            Using dr As IDataReader = db.GetProperties(PropertyId, PortalId)
                While dr.Read
                    pi = FillObject(dr)
                End While
                dr.Close()
            End Using
            Return pi
        End Function
        Private Function FillObject(ByVal dr As IDataRecord) As PropertiesInfo
            Dim pi As PropertiesInfo = Nothing
            If Not dr Is Nothing Then
                pi = New PropertiesInfo
                With pi
                    .PropertyId = CInt(dr("PropertyId").ToString)
                    .PortalId = CInt(dr("PortalId").ToString)
                    .ObjectType = CInt(dr("ObjectType").ToString)
                    .ObjectOwnerId = CInt(dr("ObjectOwnerId").ToString)
                    .Name = dr("Name").ToString
                    .DataType = dr("DataType").ToString
                    .DefaultAccessControl = CInt(dr("DefaultAccessControl").ToString)
                    .IsHidden = Boolean.Parse(dr("IsHidden").ToString)
                    .IsReadOnly = Boolean.Parse(dr("IsReadOnly").ToString)
                    .IsRequired = Boolean.Parse(dr("IsRequired").ToString)
                    .ValidationExpression = dr("ValidationExpression").ToString
                    .ViewTemplate = dr("ViewTemplate").ToString
                    .EditTemplate = dr("EditTemplate").ToString
                    .SortOrder = Integer.Parse(dr("SortOrder").ToString)
                    .DefaultValue = dr("DefaultValue").ToString
                End With
            End If
            Return pi
        End Function
    End Class
End Namespace